package org.ldh.web.servlet;

import org.ldh.domain.BookType;
import org.ldh.service.BookTypeService;
import org.ldh.service.impl.BookTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/bookTypeServlet.action")
public class BookTypeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("------BookTypeServlet------");
        //设置编码
        request.setCharacterEncoding("utf-8");
        //获取请求参数
        String action = request.getParameter("action");
        System.out.println("action: "+action);
        //调用service层
        BookTypeService bookTypeService = new BookTypeServiceImpl();

        //判断请求参数
        if(action != null && action.equals("list")){
            //查询列表
            List<BookType> bookTypes = bookTypeService.findAll();
            //存储到request域
            request.setAttribute("bookTypes",bookTypes);
            //跳转图书类别列表页面
            request.setAttribute("mainRight","/WEB-INF/pages/bookTypeList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("add")){
            //添加
            //跳转到添加页面
            request.setAttribute("mainRight","/WEB-INF/pages/bookTypeAdd.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("save")){
            //保存
            //获取请求参数
            String bookTypeId = request.getParameter("bookTypeId");
            String bookTypeName = request.getParameter("bookTypeName");
            //根据id查询
            BookType bookType = bookTypeService.findById(bookTypeId);
            if(bookType == null){
                //说明数据库中不存在该类别

                //封装实体类
                BookType bookType2 = new BookType();
                bookType2.setBookTypeId(bookTypeId);
                bookType2.setBookTypeName(bookTypeName);
                //保存图书类别
                bookTypeService.save(bookType2);
                //保存成功后，重新查询数据库
                List<BookType> bookTypes = bookTypeService.findAll();
                //重新存储到request域
                request.setAttribute("bookTypes",bookTypes);
                //跳转图书类别列表页面
                request.setAttribute("mainRight","/WEB-INF/pages/bookTypeList.jsp");
                request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
            }else{
                //说明该图书类别在数据库中已经存在
                //存储错误信息到request域，在前端显示
                request.setAttribute("error","该图书类别已经存在");
                //跳转到添加页面
                request.setAttribute("mainRight","/WEB-INF/pages/bookTypeAdd.jsp");
                request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
            }
        }else if(action != null && action.equals("update")){
            //修改
            //获取请求参数
            String bookTypeId = request.getParameter("bookTypeId");
            //根据id查询
            BookType bookType = bookTypeService.findById(bookTypeId);
            //获取该图书类别的一些信息
            String bookTypeName = bookType.getBookTypeName();
            //将信息存储到request域
            request.setAttribute("bookTypeId",bookTypeId);
            request.setAttribute("bookTypeName",bookTypeName);
            //跳转到修改页面
            request.setAttribute("mainRight","/WEB-INF/pages/bookTypeUpdate.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("reUpdate")){
            //获取请求参数
            String bookTypeId = request.getParameter("bookTypeId");
            String bookTypeName = request.getParameter("bookTypeName");
            System.out.println(bookTypeId+"::"+bookTypeName);
            //封装实体类
            BookType bookType = new BookType();
            bookType.setBookTypeId(bookTypeId);
            bookType.setBookTypeName(bookTypeName);
            System.out.println(bookType);
            //调用service层的修改方法
            bookTypeService.update(bookType);
            //修改完成之后，重新查询数据库
            List<BookType> bookTypes = bookTypeService.findAll();
            //重新存储到request域
            request.setAttribute("bookTypes",bookTypes);
            //跳转图书类别列表页面
            request.setAttribute("mainRight","/WEB-INF/pages/bookTypeList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("delete")){
            //获取请求参数
            String bookTypeId = request.getParameter("bookTypeId");
            //根据id删除
            bookTypeService.delete(bookTypeId);
            //删除完成之后，重新查询数据库
            List<BookType> bookTypes = bookTypeService.findAll();
            //重新存储到request域
            request.setAttribute("bookTypes",bookTypes);
            //跳转图书类别列表页面
            request.setAttribute("mainRight","/WEB-INF/pages/bookTypeList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
